package org.jim.core.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.jim.core.JimVersion;
public class ValidJdbcUtil {
	
	private static final String DEFAULT_CLASSPATH_CONFIGURATION_FILE = "jim.properties";
	
	public Properties getProperties() {
		ClassLoader standardClassloader = Thread.currentThread().getContextClassLoader();
        URL url = null;
        if (standardClassloader != null) {
            url = standardClassloader.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE);
        }
        if (url == null) {
        	url = JimVersion.class.getResource(DEFAULT_CLASSPATH_CONFIGURATION_FILE);
        }
        Properties prop = new Properties();
        try {
			InputStream input = url.openStream();
            prop.load(input);
            prop.getProperty("jim.redis.host","");
		} catch (IOException e) {
			e.printStackTrace();
		}
        return prop;
	}
	
	

	public boolean validation(String im_appid,String im_appkey) {
		
		Properties prop = getProperties();
		String URL = prop.getProperty("jdbc.url", "");
		String USER = prop.getProperty("jdbc.user", "");
		String PASSWORD = prop.getProperty("jdbc.password", "");
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
			String sql = "select im_appid,im_appkey from allows_client_info where im_appid=" + im_appid;
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			String appkey = "";
			while (rs.next()) {
				appkey = rs.getString(2);
			}
			if (appkey.equals(im_appkey)) {
				return true;
			}
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
}
